#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from utils.DateUtil import DateUtil
import threading
import random
import time
import json

# 上传流量报文
class Flow(threading.Thread):

    def __init__(self, logger, mqtt, deviceInfo):
        super(Flow, self).__init__()
        self.logger = logger
        self.mqtt = mqtt
        self.sn = deviceInfo["sn"]
        self.periodic = deviceInfo["periodic"]
        self.logger.info("[%s] start flow" % deviceInfo["sn"])

    def run(self):
        recv = 0 # 单位K
        send = 0 # 单位K
        while True:
            recv = recv + random.randint(1, 100)
            send = send + random.randint(1, 100)
            flow = {
                "sn": self.sn,
                "networkCard": "eth0",
                "recv": recv,
                "send": send,
                "uploadTime": DateUtil.getNowDate()
            }
            self.mqtt.publish("FLOW_LOG", json.dumps(flow, ensure_ascii=False), 2)
            self.logger.info("[%s] upload flow complete" % self.sn)
            time.sleep(self.periodic)
